package com.glory.dgcpcpda.activity.cpc;

import android.os.Build;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.activity.mfg.QCWorkActivity;
import com.glory.dgcpcpda.adapter.LotQcBatchListAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.AdrefListEntity;
import com.glory.dgcpcpda.entity.HOLDEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.LotList;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class CpcEqcDataActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.tvLotState)
    TextView tvLotState;
    @BindView(R.id.etLot)
    FreeUI_ClearEditText etLot;
    @BindView(R.id.lotQty)
    TextView lotQty;
    @BindView(R.id.tvStepDesc)
    TextView tvStepDesc;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etUserName)
    FreeUI_ClearEditText etUserName;
    @BindView(R.id.tvLotHoldState)
    TextView tvLotHoldState;
    @BindView(R.id.etEqcDate)
    FreeUI_ClearEditText etEqcDate;
    @BindView(R.id.elResult)
    TextView elResult;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_eqc_data;
    }

    @Override
    protected void initView() {
        tvTitle.setText("EQC数据收集");
        setMainLayout(llMain);

        initEdit(etLot, s -> {
            //获取lot信息
            lotInfo(s);
        });


    }

    @Override
    protected void initData() {

    }
    @OnClick(R.id.tvBaseSubmit)
    public void onViewClicked() {
        if (actionControl("Submit23"))
            return;

        if("".equals(etEqcDate.getText().toString())|| Integer.valueOf(etEqcDate.getText().toString())>800){
            showToast("EQC数量不能为空且不能大于800，请重新填写！",false);
            return;
        }

   /*     if(!tvStepDesc.getText().toString().contains("测试")){
            showToast("该批次不再测试工序，不允许收集EQC数据！",false);
            etLot.setText("");
            etLot.setFocusable(true);
            return;
        }
        if(!"正在作业".equals(tvLotState.getText().toString())){
            showToast("该批次未开始作业，不允许收集EQC数据！",false);
            return;
        }*/


        if (getText(etUserName).equals("")){
            showToast("请输入工号",false);
            return;
        }

        FreeUI_EntityCallBack<BaseResponseBean<LotInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<LotInfoEntity>>
                (new TypeToken<BaseResponseBean<LotInfoEntity>>() {
                }.getType(),  getSupportFragmentManager(), CpcEqcDataActivity.this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                    showToast("EQC数据收集成功",true);
                    //System.out.println("lotId："+response.body().getResponse().getBody().getLOTLIST().get(0).getLOTID());
                    if(800>Integer.valueOf(etEqcDate.getText().toString())){
                        elResult.setText("收集数据成功！执行EQC数量为："+etEqcDate.getText().toString()+"\n"+"EQC良率不达标，批次已被暂扣。");
                    }else{
                        elResult.setText("收集数据成功！执行EQC数量为："+etEqcDate.getText().toString());
                    }


                }
            }

            @Override
            public void onError(Response<BaseResponseBean<LotInfoEntity>> response) {
                super.onError(response);
                FreeApi_LogUtils.saveErrorMessages(response.getException(), "EQCOLLECT");
                FreeApi_ToastUtils.showFreeToast("连接服务器失败",getApplicationContext(),false,0);
            }
        };

        String isHolded="N";
        if(800>Integer.valueOf(etEqcDate.getText().toString())){
            isHolded="Y";
        }
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","FTEQCOLLECT");
        map.addProperty("LOTID",etLot.getText().toString());
        map.addProperty("EQCQTY",etEqcDate.getText().toString());
        map.addProperty("ISHOLDED",isHolded);

      //  NetUtils.mRequestPostNet("CPC.EQCOLLECT",getText(etUserName),"EQCOLLECT",getApplicationContext(),map,callBack);
        NetUtils.requestPostNet("CPC.EQCOLLECT","TRANSFERMES",getApplicationContext(),map,callBack);
    }


    private void lotInfo(String lotId){
        LotUtils.searchLotInfo(CpcEqcDataActivity.this, lotId, getSupportFragmentManager(), new LotUtils.netCallback<LotInfoEntity>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onSuccess(LotInfoEntity response) {
                    //tvTitle.setText("开始作业" + "(" + response.getLOT().getSTEPDESC() + ")");
                    lotQty.setText(response.getLOT().getMAINQTY());
                    String state = response.getLOT().getSTATE();
                    etEqcDate.setText(response.getEQCQTY());

                    tvLotHoldState.setText(response.getLOT().getHOLDSTATE());
                    tvLotState.setText(state);
                    tvStepDesc.setText(response.getLOT().getSTEPDESC());

            }
            @Override
            public void onFail(Throwable t) {
                loadError(t, "LOTQUERY");
                showConfirmDialog(CpcEqcDataActivity.this,"接口不通");
            }
        });
    }
    private void clean(){
        etLot.setText("");
        lotQty.setText("");
        tvStepDesc.setText("");
        tvLotState.setText("");
        tvLotHoldState.setText("");
    }
}
